package pearson.automation.assets;

import org.openqa.selenium.By;
import org.openqa.selenium.interactions.Actions;

import framework.elements.BaseElement;
import framework.elements.Label;
import framework.elements.Link;

import pearson.automation.forms.AssetDetailsForm;
import pearson.automation.forms.CourseSelectDialog;
import pearson.automation.forms.LightBoxForm;

public class AssetWithRollOver extends AssetWithOutRollOver {
    
    private Label pointer;
    private Link rolloverTitle;
    private Label origin;
    
    public AssetWithRollOver(String assetName) {
        super(assetName);
        declareControls();
    }
    
    public AssetWithRollOver(String assetName, int index) {
        super(assetName, index);
        declareControls();
    }

    public AssetWithRollOver(String xpath, boolean byXPath) {
        super(xpath, byXPath);
        declareControls();
    }

    public void declareControls() {
        pointer = new Label(By.xpath(String.format("%1$s//%2$s",
                assetXPath, "div[@class='pointer']")), "Image");
        rolloverTitle   = new Link(By.xpath(String.format("%1$s//%2$s",
                assetXPath, "div[@class='item-details']//a[@data-item-title]")),
                "Title");
        origin  = new Label(By.xpath(String.format("%1$s//%2$s",
                assetXPath, "h2/a")), "Origin");
    }

    @Override
    protected String formatLogMsg(String message) {
        return "Asset With Roll Up";
    }
    
    public void openAssetDetailsFromRollOver() {
        if (hasElementInRollOver(rolloverTitle)) {
            Actions builder = new Actions(browser.getDriver());    
            builder        
                .click(browser.getDriver().findElement(rolloverTitle.getLocator()))
                .build().perform();
        }
    }
    
    public boolean hasElementInRollOver(BaseElement el) {
        Actions builder = new Actions(browser.getDriver());    
        builder.moveToElement(element.getElement()).build().perform();    
        return el.isPresent();
    }
    
    public void clickElementInRollOver(BaseElement button) {
        if (hasElementInRollOver(button)) {
            Actions builder = new Actions(browser.getDriver());    
            builder        
                .click(browser.getDriver().findElement(button.getLocator()))
                .moveByOffset(-500, -500)
                .build().perform();
        }
    }
    
    public boolean hasAddToLibraryButtonInRollOver() {
        return hasElementInRollOver(addToLibrary);
    }
    
    public void waitForAddToLibraryButtonToDissappear() {
        addToLibrary.waitForElementDissapear();
    }
    
    public boolean hasPreviewButtonInRollOver() {
        return hasElementInRollOver(preview);
    }
    
    public boolean hasNewButtonInRollOver() {
        return hasElementInRollOver(newCourse);
    }
    
    public boolean hasEditButtonInRollOver() {
        return hasElementInRollOver(edit);
    }
    
    public boolean hasAddToButtonInRollOver() {
        return hasElementInRollOver(addTo);
    }
    
    public boolean hasRemoveFromLibraryButtonInRollOver() {
        return hasElementInRollOver(remove);
    }
    
    public boolean hasTitleInRollOver() {
        return hasElementInRollOver(rolloverTitle);
    }
    
    public boolean hasOriginInRollOver() {
        return hasElementInRollOver(origin);
    }

    public void clickAddToLibraryButtonFromRollOver() {
        clickElementInRollOver(addToLibrary);
    }
    
    public void clickRemoveFromLibraryButtonFromRollOver() {
        clickElementInRollOver(remove);
    }
    
    public CourseSelectDialog clickAddToButtonFromRollOver() {
        clickElementInRollOver(addTo);
        return new CourseSelectDialog();
    }

    public LightBoxForm clickPreviewButtonFromRollOver() {
        clickElementInRollOver(preview);
        return new LightBoxForm();
    }
    
    public AssetDetailsForm clickTitleFromRollOver() {
        clickElementInRollOver(rolloverTitle);
        return new AssetDetailsForm();
    }
    
    public String getTitleFromRollover() {
        if (hasTitleInRollOver()) {
            return rolloverTitle.getElement().getText();
        } else {
            return null;
        }
    }
    
    public String getOriginFromRollover() {
        if (hasOriginInRollOver()) {
            return origin.getElement().getText();
        } else {
            return null;
        }
    }
    
    public String getOriginLinkFromRollOver() {
        if (hasOriginInRollOver()) {
            return origin.getElement().getAttribute("href");
        } else {
            return null;
        }
    }
    
    public boolean hasRollOver() {
        if (hasElementInRollOver(pointer)) {
            return pointer.getElement().getCssValue("display").equals("block");
        } else {
            return false;
        }
    }

}
